/*******************************************************************************
 * $Header: /cvsroot/oloa/org.gocom.bpm.platform.model/src/org/gocom/bpm/platform/model/web/taglib/ProcessGraphTag.java,v 1.1 2011/07/03 02:30:16 hufulai Exp $
 * $Revision: 1.1 $
 * $Date: 2011/07/03 02:30:16 $
 *
 *==============================================================================
 *
 * Copyright (c) 2001-2006 Primeton Technologies, Ltd.
 * All rights reserved.
 * 
 * Created on 2010-5-26
 *******************************************************************************/

package com.atozsoft.wf.taglib;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;





import com.eos.web.taglib.util.XpathUtil;
import com.primeton.workflow.commons.config.WFConfigurationHelper;
import com.primeton.workflow.commons.utility.StringUtil;
import com.primeton.workflow.model.definition.WFProcess;
import com.atozsoft.wf.web.IProcessObjectProvider;
import com.atozsoft.wf.web.ProcessObjectFactory;
import com.atozsoft.wf.web.IProcessObjectProvider.ProcessObject;


public class ProcessGraphTag extends WFHTMLBaseTag{
	
    /**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	
	private String processInstID;
    private String processName;
    private String processID;
    private String zoomQuotiety;
    private String titlePath;
    private String iconPath;
    
    //��ʾע��
    private String showNote;
    //���̶����ṩ������
    private String providerClassName;
    //����ʵ����
    private String processInstName;
    
    private long m_processInstID;
    private long m_processDefID;
    private double m_zoomQuotiety;
    private IGraphics graphics;
    
    IProcessObjectProvider processProvider;
    
    public ProcessGraphTag()
    {
        processInstID = null;
        processName = null;
        processInstName=null;
        processID = null;
        zoomQuotiety = "1.0";
        titlePath = null;
        iconPath = null;
        m_zoomQuotiety = 1.0D;
        
        
    }

    public String getIconPath()
    {
        return iconPath;
    }

    public void setIconPath(String iconPath)
    {
        this.iconPath = iconPath;
    }

    public String getProcessID()
    {
        return processID;
    }

    public void setProcessID(String processDefId)
    {
        processID = processDefId;
    }

    public String getProcessName()
    {
        return processName;
    }

    public void setProcessName(String processDefName)
    {
        processName = processDefName;
    }

    public String getProcessInstID()
    {
        return processInstID;
    }

    public void setProcessInstID(String processInstId)
    {
        processInstID = processInstId;
    }

    public String getTitlePath()
    {
        return titlePath;
    }

    public void setTitlePath(String titlePath)
    {
        this.titlePath = titlePath;
    }

    public String getZoomQuotiety()
    {
        return zoomQuotiety;
    }

    public void setZoomQuotiety(String zoomQuotiety)
    {
        this.zoomQuotiety = zoomQuotiety;
    }

    
    public String getProviderClassName() {
		return providerClassName;
	}

	public void setProviderClassName(String providerClassName) {
		this.providerClassName = providerClassName;
	}

	public String getShowNote() {
		return showNote;
	}

	public void setShowNote(String showNote) {
		this.showNote = showNote;
	}

	public void initVariables()
    {
        try
        {
        	processProvider=ProcessObjectFactory.getProcessObjectProvider(XpathUtil.getStringByXpathSupport(getRootObj(),getProviderClassName()));
        	
        	setShowNote(XpathUtil.getStringByXpathSupport(getRootObj(),getShowNote()));
            setProcessInstID(XpathUtil.getStringByXpathSupport(getRootObj(), getProcessInstID()));
            setProcessName(XpathUtil.getStringByXpathSupport(getRootObj(), getProcessName()));
            setProcessID(XpathUtil.getStringByXpathSupport(getRootObj(), getProcessID()));
            setZoomQuotiety(XpathUtil.getStringByXpathSupport(getRootObj(), getZoomQuotiety()));
            setOnclick(XpathUtil.getStringByXpathSupport(getRootObj(), getOnclick()));
            setOndblclick(XpathUtil.getStringByXpathSupport(getRootObj(), getOndblclick()));
            setOnmouseover(XpathUtil.getStringByXpathSupport(getRootObj(), getOnmouseover()));
            setOnmouseout(XpathUtil.getStringByXpathSupport(getRootObj(), getOnmouseout()));
            if(StringUtil.isNotNullAndBlank(processInstID))
                m_processInstID = Long.parseLong(processInstID);
            if(StringUtil.isNotNullAndBlank(processID))
                m_processDefID = Long.parseLong(processID);
            if(StringUtil.isNotNullAndBlank(zoomQuotiety))
                m_zoomQuotiety = Double.parseDouble(zoomQuotiety);
        }
        catch(JspException e)
        {
            e.printStackTrace();
        }
    }

    public int doStartTag() throws JspException
    {
        HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
        ScriptGenerator scriptgenor=null;
        initScope();
        initVariables();
        ProcessObject processObject=processProvider.getProcessObject(m_processInstID, processName, m_processDefID);
        WFProcess process=processObject.getProcess();
        if(process == null)
           throw new JspException("not found process!");
        
        try
        {
            scriptgenor = new ScriptGenerator(0, 0, 1, m_zoomQuotiety, request.getContextPath());
            scriptgenor.setProcessDefName(process.getId());
            scriptgenor.setWebAppPath(request.getSession().getServletContext().getRealPath("/"));
            graphics = new GraphicsImpl(processObject.getProcessDefID(), process, scriptgenor, this);
        }catch(Exception e){
            throw new JspException(e);
        }finally{
        }

        if(processObject.getProcessInstID() !=-1){
            try{
            	processInstName=processObject.getProcessInstName();
            	
                scriptgenor.setImagePath((new StringBuilder(String.valueOf(request.getContextPath()))).append(WFConfigurationHelper.retrieveWFConfiguration().getProcessInstGraphImagePath()).toString());
                processProvider.transform2ProcessInstGraph(graphics.getConnections(), graphics.getStates(), processObject);
                
            } catch(Exception e){
            	e.printStackTrace();
                throw new JspException(e);
            }
        }
        return 1;
    }

    public int doAfterBody() throws JspException
    {
        return !getGraphics().hasNextState() ? 0 : 2;
    }

    
    public String getProcessInstName() {
		return processInstName;
	}

    
	public int doEndTag() throws JspException
    {
        StringBuffer out = new StringBuffer();
        //graphics.drawHeader(out);
        
        if("true".equalsIgnoreCase(showNote))
        	graphics.drawNotes(out);
        
        graphics.drawStates(out);

        out.append("<script language=\"javascript\">\r\n");
        StringBuffer lineText = new StringBuffer();
        graphics.drawConnections(out, lineText);
        out.append("</script>\r\n");
        out.append(lineText);
        
        try
        {
            pageContext.getOut().print(out);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        release();
        return 6;
    }

    public IGraphics getGraphics()
    {
        return graphics;
    }

    public void assembleActivityIcon(State state)throws JspException
    {
        if(StringUtil.isNotNullAndBlank(getIconPath()))
            if(getIconPath().endsWith("/"))
                state.setImgSrc(XpathUtil.getStringByXpathSupport(getRootObj(), (new StringBuilder(String.valueOf(getIconPath()))).append(state.getType()).toString()));
            else
                state.setImgSrc(XpathUtil.getStringByXpathSupport(getRootObj(), (new StringBuilder(String.valueOf(getIconPath()))).append("/").append(state.getType()).toString()));
    }

    public void assembleActivityTitle(State state) throws JspException
    {
        if(StringUtil.isNotNullAndBlank(getTitlePath()))
            if(getTitlePath().endsWith("/"))
                state.setName(XpathUtil.getStringByXpathSupport(getRootObj(), (new StringBuilder(String.valueOf(getTitlePath()))).append(state.getID()).toString()));
            else
                state.setName(XpathUtil.getStringByXpathSupport(getRootObj(), (new StringBuilder(String.valueOf(getTitlePath()))).append("/").append(state.getID()).toString()));
    }

    public void assembleMouseEvents(WFHTMLBaseTag tag, State state)
    {
        StringBuffer mouseEvents = new StringBuffer();
        if(StringUtil.isNotNullAndBlank(getOnclick()))
            prepareAttribute(mouseEvents, "onClick", tag.getOnclick());
        else if(StringUtil.isNotNullAndBlank(tag.getOnclick()))
            prepareAttribute(mouseEvents, "onClick", tag.getOnclick());
        
        if(StringUtil.isNotNullAndBlank(getOndblclick()))
            prepareAttribute(mouseEvents, "onDblclick", tag.getOndblclick());
        else if(StringUtil.isNotNullAndBlank(tag.getOnclick()))
            prepareAttribute(mouseEvents, "onDblclick", tag.getOndblclick());
        
        if(StringUtil.isNotNullAndBlank(tag.getOnmouseover()))
            prepareAttribute(mouseEvents, "onMouseOver", tag.getOnmouseover());
        else if(StringUtil.isNotNullAndBlank(tag.getOnclick()))
            prepareAttribute(mouseEvents, "onMouseOver", tag.getOnmouseover());
        
        if(StringUtil.isNotNullAndBlank(tag.getOnmouseout()))
            prepareAttribute(mouseEvents, "onMouseOut", tag.getOnmouseout());
        else if(StringUtil.isNotNullAndBlank(tag.getOnclick()))
            prepareAttribute(mouseEvents, "onMouseOut", tag.getOnmouseout());
        
        state.setMouseEvents(mouseEvents.toString());
    }

    public void release()
    {
        super.release();
        processInstID = null;
        processName = null;
        processID = null;
        processInstName=null;
        zoomQuotiety = "1.0";
        titlePath = null;
        iconPath = null;
        m_processInstID = 0L;
        m_processDefID = 0L;
        m_zoomQuotiety = 1.0D;
        
    }



}
